package com.rbh.risk.managedbean.frontend;

import com.rbh.risk.entity.Category;
import com.rbh.risk.entity.DrugItems;
import com.rbh.risk.entity.MainCause;
import com.rbh.risk.entity.MainEvent;
import com.rbh.risk.entity.Patient;
import com.rbh.risk.entity.PersonImpacted;
import com.rbh.risk.entity.RiskData;
import com.rbh.risk.entity.SeverityLevelDetail;
import com.rbh.risk.entity.SubCause;
import com.rbh.risk.entity.SubEvent;
import com.rbh.risk.entity.Workgroup;
import com.rbh.risk.service.AuthenticationService;
import com.rbh.risk.service.CategoryService;
import com.rbh.risk.service.DrugItemsService;
import com.rbh.risk.service.MainCauseService;
import com.rbh.risk.service.MainEventService;
import com.rbh.risk.service.PatientService;
import com.rbh.risk.service.PersonImpactedService;
import com.rbh.risk.service.RiskDataService;
import com.rbh.risk.service.SeverityLevelDetailService;
import com.rbh.risk.service.SeverityLevelService;
import com.rbh.risk.service.SubCauseService;
import com.rbh.risk.service.SubEventService;
import com.rbh.risk.service.WorkgroupService;
import com.rbh.risk.util.Constants;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author M
 */
@ManagedBean(name = "riskAddBean")
@SessionScoped
public class RiskAddBean implements Serializable {

    private static final long serialVersionUID = 1L;
    /* Injection Attribute */
    @ManagedProperty(value = "#{riskData}")
    private RiskData riskData;
    /* Input Object */
    @ManagedProperty(value = "#{selectedDateTime}")
    private Date selectedDateTime;
    @ManagedProperty(value = "#{txtLocation}")
    private String txtLocation;
    @ManagedProperty(value = "#{txtEventDetails}")
    private String txtEventDetails;
    @ManagedProperty(value = "#{txtPreliminaryResolve}")
    private String txtPreliminaryResolve;
    @ManagedProperty(value = "#{selectedWorkgroup}")
    private Workgroup selectedWorkgroup;
    @ManagedProperty(value = "#{selectedWorkgroupConnected}")
    private Workgroup selectedWorkgroupConnected;
    @ManagedProperty(value = "#{selectedCategory}")
    private Category selectedCategory;
    @ManagedProperty(value = "#{selectedMainEvent}")
    private MainEvent selectedMainEvent;
    @ManagedProperty(value = "#{selectedSubEvent}")
    private SubEvent selectedSubEvent;
    @ManagedProperty(value = "#{selectedPersonImpactedList}")
    private List<PersonImpacted> selectedPersonImpactedList;
    @ManagedProperty(value = "#{selectedPatient}")
    private Patient selectedPatient;
    @ManagedProperty(value = "#{selectedSeverityLevelDetail}")
    private SeverityLevelDetail selectedSeverityLevelDetail;
    @ManagedProperty(value = "#{selectedMainCause}")
    private MainCause selectedMainCause;
    @ManagedProperty(value = "#{selectedSubCause}")
    private SubCause selectedSubCause;
    /* Optional Input Object */
    @ManagedProperty(value = "#{selectedDrugTrue}")
    private DrugItems selectedDrugTrue;
    @ManagedProperty(value = "#{selectedDrugFalse}")
    private DrugItems selectedDrugFalse;
    @ManagedProperty(value = "#{isMedError}")
    private boolean isMedError;
    /* Objects */
    @ManagedProperty(value = "#{workgroupList}")
    private List<Workgroup> workgroupList;
    @ManagedProperty(value = "#{categoryList}")
    private List<Category> categoryList;
    @ManagedProperty(value = "#{mainEventList}")
    private List<MainEvent> mainEventList;
    @ManagedProperty(value = "#{subEventList}")
    private List<SubEvent> subEventList;
    @ManagedProperty(value = "#{personImpactedList}")
    private List<PersonImpacted> personImpactedList;
    @ManagedProperty(value = "#{severityLevelDetailsList}")
    private List<SeverityLevelDetail> severityLevelDetailsList;
    @ManagedProperty(value = "#{mainCauseList}")
    private List<MainCause> mainCauseList;
    @ManagedProperty(value = "#{subCauseList}")
    private List<SubCause> subCauseList;
    /* Bussiness Logic */
    @ManagedProperty(value = "#{authenticationService}")
    private AuthenticationService authenticationService;
    @ManagedProperty(value = "#{riskDataService}")
    private RiskDataService riskDataService;
    @ManagedProperty(value = "#{workgroupService}")
    private WorkgroupService workgroupService;
    @ManagedProperty(value = "#{categoryService}")
    private CategoryService categoryService;
    @ManagedProperty(value = "#{mainEventService}")
    private MainEventService mainEventService;
    @ManagedProperty(value = "#{subEventService}")
    private SubEventService subEventService;
    @ManagedProperty(value = "#{personImpactedService}")
    private PersonImpactedService personImpactedService;
    @ManagedProperty(value = "#{severityLevelService}")
    private SeverityLevelService severityLevelService;
    @ManagedProperty(value = "#{severityLevelDetailService}")
    private SeverityLevelDetailService severityLevelDetailService;
    @ManagedProperty(value = "#{mainCauseService}")
    private MainCauseService mainCauseService;
    @ManagedProperty(value = "#{subCauseService}")
    private SubCauseService subCauseService;
    @ManagedProperty(value = "#{drugItemsService}")
    private DrugItemsService drugItemsService;
    @ManagedProperty(value = "#{patientService}")
    private PatientService patientService;
    @ManagedProperty(value = "#{maxDate}")
    private Date maxDate;
    /* Injection Backing Bean */
    @ManagedProperty(value = "#{privilegeBean}")
    private PrivilegeBean privilegeBean;

    public RiskAddBean() {
    }

    @PostConstruct
    public void init() {
        selectedDateTime = new Date();
        isMedError = false;
    }

    public String injectedRiskData(RiskData data) throws Exception {
        selectedCategory = data.getCategory();
        selectedDateTime = data.getDataAccidentDatetime();
        selectedMainCause = data.getMainCause();
        selectedMainEvent = data.getMainEvent();
        selectedSeverityLevelDetail = data.getSeverityLevelDetail();
        selectedSubCause = data.getSubCause();
        selectedSubEvent = data.getSubEvent();
        selectedWorkgroup = data.getWorkgroup();
        selectedWorkgroupConnected = data.getWorkgroupConnected();
        txtEventDetails = data.getDataEventDetails();
        txtLocation = data.getDataLocate();
        txtPreliminaryResolve = data.getDataPreliminaryResolve();
        selectedPatient = data.getPatient();
        selectedDrugTrue = data.getDrugItemsTrue();
        selectedDrugFalse = data.getDrugItemsFalse();
        if (data.getPersonImpactedList() != null && !data.getPersonImpactedList().isEmpty()) {
            selectedPersonImpactedList = new ArrayList<>();
            for (PersonImpacted impact : data.getPersonImpactedList()) {
                selectedPersonImpactedList.add(personImpactedService.getById(impact.getPersonImpactedId()));
            }
        }

        /* setting list for choose */
        subCauseList = subCauseService.getByMainCause(data.getMainCause());
        severityLevelDetailsList = severityLevelDetailService.getByCategory(data.getCategory());

        if (data.getCategory().getCategoryId().intValue() == 3) {
            isMedError = true;
        } else {
            isMedError = false;
        }

        // For Reference
        riskData = data;

        return Constants.PATH_ADD;
    }

    /*
     * Method For AutoComplete
     */
    public List<Workgroup> completeWorkgroup(String words) {
        List<Workgroup> results;
        if (selectedWorkgroup != null) {
            results = workgroupService.getByNotEqWords(words, selectedWorkgroup);
        } else {
            results = workgroupService.getByWords(words);
        }
        return results;
    }

    public List<SubEvent> completeSubEvent(String words) {
        return subEventService.getByWords(words);
    }

    public List<Patient> completePatient(String words) {
        return patientService.getByWords(words);
    }

    public List<DrugItems> completeDrugItems(String words) throws Exception {
        return drugItemsService.getByWords(words);
    }

    /**
     * Method Zone
     */
    public void handleSelectionRelate(final String choice) throws Exception {
        switch (choice) {
            case "subCause":
                selectedSubCause = null;
                subCauseList = null;
                subCauseList = subCauseService.getByMainCause(selectedMainCause);
                break;
        }
    }

    public void handleAutoComplete(final String choice) throws Exception {
        switch (choice) {
            case "subEvent":
                selectedMainEvent = selectedSubEvent.getMainEvent();
                selectedCategory = selectedMainEvent.getCategory();
                severityLevelDetailsList = severityLevelDetailService.getByCategory(selectedCategory);
                if (selectedCategory.getCategoryId().intValue() == 3) {
                    isMedError = true;
                } else {
                    isMedError = false;
                }
                break;
        }
    }

    public void handleDateSelect(SelectEvent event) {
        selectedDateTime = (Date) event.getObject();
    }

    public String saveAs() {
        this.save();
        return Constants.PATH_REPORT;
    }

    public void save() {
        RiskData risk = new RiskData();
        risk.setProfileCreate(authenticationService.getPrincipal().getProfile());
        risk.setDataAccidentDatetime(selectedDateTime);
        risk.setWorkgroup(selectedWorkgroup);
        risk.setWorkgroupConnected(selectedWorkgroupConnected);
        risk.setCategory(selectedCategory);
        risk.setMainEvent(selectedMainEvent);
        risk.setSubEvent(selectedSubEvent);
        risk.setPersonImpactedList(selectedPersonImpactedList);
        risk.setPatient(selectedPatient);
        risk.setDrugItemsTrue(selectedDrugTrue);
        risk.setDrugItemsFalse(selectedDrugFalse);
        risk.setDataLocate(txtLocation);
        risk.setDataEventDetails(txtEventDetails);
        risk.setDataPreliminaryResolve(txtPreliminaryResolve);
        risk.setSeverityLevelDetail(selectedSeverityLevelDetail);
        risk.setMainCause(selectedMainCause);
        risk.setSubCause(selectedSubCause);

        try {
            riskDataService.insert(risk);
            this.destroy();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "บันทึกข้อมูลความเสี่ยงสำเร็จ"));
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failure", ex.getMessage()));
        }
    }

    public void update() {
        riskData.setProfileCreate(authenticationService.getPrincipal().getProfile());
        riskData.setDataAccidentDatetime(selectedDateTime);
        riskData.setWorkgroup(selectedWorkgroup);
        riskData.setWorkgroupConnected(selectedWorkgroupConnected);
        riskData.setCategory(selectedCategory);
        riskData.setMainEvent(selectedMainEvent);
        riskData.setSubEvent(selectedSubEvent);
        riskData.setPersonImpactedList(selectedPersonImpactedList);
        riskData.setPatient(selectedPatient);
        riskData.setDrugItemsTrue(selectedDrugTrue);
        riskData.setDrugItemsFalse(selectedDrugFalse);
        riskData.setDataLocate(txtLocation);
        riskData.setDataEventDetails(txtEventDetails);
        riskData.setDataPreliminaryResolve(txtPreliminaryResolve);
        riskData.setSeverityLevelDetail(selectedSeverityLevelDetail);
        riskData.setMainCause(selectedMainCause);
        riskData.setSubCause(selectedSubCause);
        try {
            riskDataService.update(riskData);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "แก้ไขข้อมูลความเสี่ยงสำเร็จ"));
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failure", ex.getMessage()));
        }
    }

    public String review() {
        try {
            riskDataService.update(privilegeBean.setReviewLevel(riskData));
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "ทบทวนข้อมูลความเสี่ยงสำเร็จ"));
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failure", ex.getMessage()));
        } finally {
            return Constants.PATH_REPORT;
        }
    }

    public boolean canReview() {
        return privilegeBean.canReview(riskData);
    }

    public String reset() throws Exception {
        return this.injectedRiskData(riskData);
    }

    public String destroy() {
        selectedDateTime = new Date();
        selectedCategory = null;
        selectedMainCause = null;
        selectedSubCause = null;
        selectedPatient = null;
        selectedWorkgroup = null;
        selectedWorkgroupConnected = null;
        selectedDrugTrue = null;
        selectedDrugFalse = null;
        selectedMainEvent = null;
        selectedSubEvent = null;
        selectedPatient = null;
        selectedDrugTrue = null;
        selectedDrugFalse = null;
        selectedPersonImpactedList = new ArrayList<>();
        selectedSeverityLevelDetail = null;
        txtLocation = null;
        txtEventDetails = null;
        txtPreliminaryResolve = null;
        isMedError = false;
        riskData = null;

        return Constants.PATH_ADD;
    }

    /* @return Zone */
    public Date getSelectedDateTime() {
        return selectedDateTime;
    }

    public void setSelectedDateTime(Date selectedDateTime) {
        this.selectedDateTime = selectedDateTime;
    }

    public String getTxtLocation() {
        return txtLocation;
    }

    public void setTxtLocation(String txtLocation) {
        this.txtLocation = txtLocation;
    }

    public String getTxtEventDetails() {
        return txtEventDetails;
    }

    public void setTxtEventDetails(String txtEventDetails) {
        this.txtEventDetails = txtEventDetails;
    }

    public String getTxtPreliminaryResolve() {
        return txtPreliminaryResolve;
    }

    public void setTxtPreliminaryResolve(String txtPreliminaryResolve) {
        this.txtPreliminaryResolve = txtPreliminaryResolve;
    }

    public Workgroup getSelectedWorkgroup() {
        return selectedWorkgroup;
    }

    public void setSelectedWorkgroup(Workgroup selectedWorkgroup) {
        this.selectedWorkgroup = selectedWorkgroup;
    }

    public Workgroup getSelectedWorkgroupConnected() {
        return selectedWorkgroupConnected;
    }

    public void setSelectedWorkgroupConnected(Workgroup selectedWorkgroupConnected) {
        this.selectedWorkgroupConnected = selectedWorkgroupConnected;
    }

    public Category getSelectedCategory() {
        return selectedCategory;
    }

    public void setSelectedCategory(Category selectedCategory) {
        this.selectedCategory = selectedCategory;
    }

    public MainEvent getSelectedMainEvent() {
        return selectedMainEvent;
    }

    public void setSelectedMainEvent(MainEvent selectedMainEvent) {
        this.selectedMainEvent = selectedMainEvent;
    }

    public SubEvent getSelectedSubEvent() {
        return selectedSubEvent;
    }

    public void setSelectedSubEvent(SubEvent selectedSubEvent) {
        this.selectedSubEvent = selectedSubEvent;
    }

    public List<PersonImpacted> getSelectedPersonImpactedList() {
        return selectedPersonImpactedList;
    }

    public void setSelectedPersonImpactedList(List<PersonImpacted> selectedPersonImpactedList) {
        this.selectedPersonImpactedList = selectedPersonImpactedList;
    }

    public SeverityLevelDetail getSelectedSeverityLevelDetail() {
        return selectedSeverityLevelDetail;
    }

    public void setSelectedSeverityLevelDetail(SeverityLevelDetail selectedSeverityLevelDetail) {
        this.selectedSeverityLevelDetail = selectedSeverityLevelDetail;
    }

    public MainCause getSelectedMainCause() {
        return selectedMainCause;
    }

    public void setSelectedMainCause(MainCause selectedMainCause) {
        this.selectedMainCause = selectedMainCause;
    }

    public SubCause getSelectedSubCause() {
        return selectedSubCause;
    }

    public void setSelectedSubCause(SubCause selectedSubCause) {
        this.selectedSubCause = selectedSubCause;
    }

    public DrugItems getSelectedDrugTrue() {
        return selectedDrugTrue;
    }

    public void setSelectedDrugTrue(DrugItems selectedDrugTrue) {
        this.selectedDrugTrue = selectedDrugTrue;
    }

    public DrugItems getSelectedDrugFalse() {
        return selectedDrugFalse;
    }

    public void setSelectedDrugFalse(DrugItems selectedDrugFalse) {
        this.selectedDrugFalse = selectedDrugFalse;
    }

    public boolean isIsMedError() {
        return isMedError;
    }

    public void setIsMedError(boolean isMedError) {
        this.isMedError = isMedError;
    }

    public List<Workgroup> getWorkgroupList() {
        return this.workgroupService.getChildWorkgroup();
    }

    public void setWorkgroupList(List<Workgroup> workgroupList) {
        this.workgroupList = workgroupList;
    }

    public List<Category> getCategoryList() {
        return categoryService.getAll();
    }

    public void setCategoryList(List<Category> categoryList) {
        this.categoryList = categoryList;
    }

    public List<MainEvent> getMainEventList() {
        return mainEventList;
    }

    public void setMainEventList(List<MainEvent> mainEventList) {
        this.mainEventList = mainEventList;
    }

    public List<SubEvent> getSubEventList() {
        return subEventService.getAll();
    }

    public void setSubEventList(List<SubEvent> subEventList) {
        this.subEventList = subEventList;
    }

    public List<PersonImpacted> getPersonImpactedList() {
        return personImpactedService.getAll();
    }

    public void setPersonImpactedList(List<PersonImpacted> personImpactedList) {
        this.personImpactedList = personImpactedList;
    }

    public List<SeverityLevelDetail> getSeverityLevelDetailsList() {
        return severityLevelDetailsList;
    }

    public void setSeverityLevelDetailsList(List<SeverityLevelDetail> severityLevelDetailsList) {
        this.severityLevelDetailsList = severityLevelDetailsList;
    }

    public List<MainCause> getMainCauseList() {
        return mainCauseService.getAll();
    }

    public void setMainCauseList(List<MainCause> mainCauseList) {
        this.mainCauseList = mainCauseList;
    }

    public List<SubCause> getSubCauseList() {
        return subCauseList;
    }

    public void setSubCauseList(List<SubCause> subCauseList) {
        this.subCauseList = subCauseList;
    }

    public AuthenticationService getAuthenticationService() {
        return authenticationService;
    }

    public void setAuthenticationService(AuthenticationService authenticationService) {
        this.authenticationService = authenticationService;
    }

    public RiskDataService getRiskDataService() {
        return riskDataService;
    }

    public void setRiskDataService(RiskDataService riskDataService) {
        this.riskDataService = riskDataService;
    }

    public WorkgroupService getWorkgroupService() {
        return workgroupService;
    }

    public void setWorkgroupService(WorkgroupService workgroupService) {
        this.workgroupService = workgroupService;
    }

    public CategoryService getCategoryService() {
        return categoryService;
    }

    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    public MainEventService getMainEventService() {
        return mainEventService;
    }

    public void setMainEventService(MainEventService mainEventService) {
        this.mainEventService = mainEventService;
    }

    public SubEventService getSubEventService() {
        return subEventService;
    }

    public void setSubEventService(SubEventService subEventService) {
        this.subEventService = subEventService;
    }

    public PersonImpactedService getPersonImpactedService() {
        return personImpactedService;
    }

    public void setPersonImpactedService(PersonImpactedService personImpactedService) {
        this.personImpactedService = personImpactedService;
    }

    public SeverityLevelService getSeverityLevelService() {
        return severityLevelService;
    }

    public void setSeverityLevelService(SeverityLevelService severityLevelService) {
        this.severityLevelService = severityLevelService;
    }

    public SeverityLevelDetailService getSeverityLevelDetailService() {
        return severityLevelDetailService;
    }

    public void setSeverityLevelDetailService(SeverityLevelDetailService severityLevelDetailService) {
        this.severityLevelDetailService = severityLevelDetailService;
    }

    public MainCauseService getMainCauseService() {
        return mainCauseService;
    }

    public void setMainCauseService(MainCauseService mainCauseService) {
        this.mainCauseService = mainCauseService;
    }

    public SubCauseService getSubCauseService() {
        return subCauseService;
    }

    public void setSubCauseService(SubCauseService subCauseService) {
        this.subCauseService = subCauseService;
    }

    public DrugItemsService getDrugItemsService() {
        return drugItemsService;
    }

    public void setDrugItemsService(DrugItemsService drugItemsService) {
        this.drugItemsService = drugItemsService;
    }

    public Date getMaxDate() {
        return new Date();
    }

    public void setMaxDate(Date maxDate) {
        this.maxDate = maxDate;
    }

    public Patient getSelectedPatient() {
        return selectedPatient;
    }

    public void setSelectedPatient(Patient selectedPatient) {
        this.selectedPatient = selectedPatient;
    }

    public RiskData getRiskData() {
        return riskData;
    }

    public void setRiskData(RiskData riskData) {
        this.riskData = riskData;
    }

    public PrivilegeBean getPrivilegeBean() {
        return privilegeBean;
    }

    public void setPrivilegeBean(PrivilegeBean privilegeBean) {
        this.privilegeBean = privilegeBean;
    }

    public PatientService getPatientService() {
        return patientService;
    }

    public void setPatientService(PatientService patientService) {
        this.patientService = patientService;
    }
}